//重置样式
require('../../assets/css/index.less')

//引入weui库
require('../../assets/lib/weui/weui.css')
let weui = require('../../assets/lib/weui/weui.js')

//当前页面样式
require('./register.less')

//引入dom
const dom = require('../../utils/dom.js')
//引入验证码插件
let CaptchaMini = require("captcha-mini")
//引入validator  //正则
const validator = require('../../utils/validator.js')
//引入axios
const https = require('../../utils/https.js')


dom.reday(function () {
    let regCode = ''
    let captcha1 = new CaptchaMini();
    captcha1.draw(document.querySelector('#captcha1'), r => {
        regCode = r.toLowerCase()
    });

    dom.getEvent('#regBtn', function () {

        //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

        //判断手机为不空
        if (!inpt1.value) {
            weui.topTips('手机号不能为空')
        }
        //判断手机号格式正确
        else if (!validator.checkPhone(inpt1.value)) {
            weui.topTips('手机号格式不正确')
        }
        //判断验证码不空
        else if (!inpt2.value) {
            weui.topTips('验证码不能为空')
        }
        //判断验证码正确
        else if ((inpt2.value).toLowerCase() != regCode) {
            weui.topTips('验证码不正确')
        }
        //判断密码不空
        else if (!inpt3.value) {
            weui.topTips('密码不能为空')
        }
        //判断密码正确
        else if (!validator.checkPassword(inpt3.value)) {
            weui.topTips('密码格式不正确')
        }
        //再次确认密码
        else if (inpt3.value != inpt4.value) {
            weui.topTips('两次密码不一致')
        }
        //通过
        else {
            //发送ajax请求
            https.post('/api/user/isExist', {
                account: inpt1.value
            }, function (res) {
                //判定 是否可用
                if (res.errno == 0) {//可用
                    https.post('/api/user/register', {
                        account: inpt1.value,
                        password: inpt3.value
                    }, function (res) {
                        //判定 是否注册成功
                        if (res.errno == 0) {//成功
                            // alert('恭喜您!注册账号成功!');
                            weui.topTips('恭喜你注册成功')
                            location.href = 'login.html'
                        } else {//失败
                            weui.topTips(res.message)
                        }
                    })
                } else {//不可用
                    weui.topTips(res.message)
                }
            })

        }
    })

})

//点击跳转密码登录页
dom.getEvent('#passwordLogin', function () {
    location.href = 'login.html'
})
